package com.bora_bandeco;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

public class Contato {
	private int id;
	private String nome;
	private String foto;
	private String numeroTelefone;
	
	public Contato() {
		this.id = 0;
		this.nome = "";
		this.foto = "";
		this.numeroTelefone = "";
	}
	
	public Contato(int id) {
		this.id = id;
		SQLiteDatabase db = BoraBandecoProvider.dbHelper.getWritableDatabase();
		
		String[] projection = { "_id", "nome", "foto", "numero_telefone" };
	    String where =  "_id = " + this.id;
		Cursor cursor = db.query(BoraBandecoProvider.TABELA_CONTATOS, projection, where, null, null, null, null, null);
		
		if (cursor != null) {
			cursor.moveToFirst();
			if (cursor.getCount() > 0) {
				this.nome = cursor.getString(cursor.getColumnIndex("nome"));
				this.foto = cursor.getString(cursor.getColumnIndex("foto"));
				this.numeroTelefone = cursor.getString(cursor.getColumnIndex("numero_telefone"));
			} else {
				this.nome = "";
				this.foto = "";
				this.numeroTelefone = "";
			}
			cursor.close();
		}
		db.close();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getFoto() {
		return foto;
	}

	public void setFoto(String foto) {
		this.foto = foto;
	}
	
	public String getNumeroTelefone() {
		return numeroTelefone;
	}

	public void setNumeroTelefone(String numeroTelefone) {
		this.numeroTelefone = numeroTelefone;
	}

	public Bitmap getBitmap() {
		Bitmap bitmap = null;
		
		if (!this.foto.equals("")) {
    		byte[] decodedString = Base64.decode(this.foto, Base64.DEFAULT);
        	bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    	}
		return bitmap;
	}
	
	public void salvar() {
		SQLiteDatabase db = BoraBandecoProvider.dbHelper.getWritableDatabase();
		String query = "";
		
		if (this.estaSalvo()) {
			query =
				"UPDATE " + BoraBandecoProvider.TABELA_CONTATOS + " SET " + 
				"nome='" + this.nome + "', "+
				"foto='" + this.foto + "', "+
				"numero_telefone='" + this.numeroTelefone + "' " +
				"WHERE _id='" + this.id + "'";
		} else {
			query =
				"INSERT INTO " + BoraBandecoProvider.TABELA_CONTATOS + " (_id, nome, foto, numero_telefone)  " +
				"SELECT " + this.id + ", '" +
							this.nome + "', '" +
							this.foto + "', '" +
							this.numeroTelefone + "'";
		}

		db.execSQL(query);
		db.close();
	}
	
	public boolean estaSalvo() {
		boolean estaSalvo = true;
		SQLiteDatabase db = BoraBandecoProvider.dbHelper.getReadableDatabase();
		
		String[] projection = { "_id" };
	    String where =  "_id = " + this.id;
		Cursor cursor = db.query(BoraBandecoProvider.TABELA_CONTATOS, projection, where, null, null, null, null, null);
		
		if (cursor.getCount() == 0) {
			estaSalvo = false;
		}
		
		cursor.close();
		
		return estaSalvo;
	}
	
}
